import { ApiCall, BaseClient, WsClient } from "tsrpc";
import { ReqMove, ResMove } from "../../../shared/master/word/PtlMove";
import { player } from "../../../model/fight/body/player";
import app from "../../../app";
import { template } from "../../../shared/master/MsgAction";
import master from "../..";
let moveMap:Map<string,number> = new Map();
export default async function (call: ApiCall<ReqMove, ResMove>) {
    call.succ({})
    let user = call.req._player as player;
    let _cell =  user.getPosition();

    // 如果在战斗中，根据word的配置 决定是否自动离开战斗
    if(!_cell.haveBreakOut(user.uuid)){
        app.sendConn(user.getConn(),{
            template: template.default,
            tolocation: call.req._fromid as string,
            data:'[提示]此位置战斗中不能离开.'
        })
        return;
    }
    if(user.is_die()){
        await user.callApi('word/ResLife',{})
    }
    let _word = _cell.getWord();
    let tag_cell = _word.getCell(_cell.x + call.req.dir[0],_cell.y +  + call.req.dir[1]);
    if(!tag_cell){
        user.callApi('word/Map',{msg:'无法移动到这里哦~'})
        return;
    }
    let lastMoveTime = moveMap.get(user.uuid) || 0;
    if(Date.now() - lastMoveTime < 3000){
        user.sendMsg(template.default,'移动太快啦，请休息一下两秒.');
        return;
    }
    moveMap.set(user.uuid,Date.now());
    
    user.setPosition(tag_cell);
    master.s_wss.callApi('word/Position',{
        _platform:call.req._platform,
        _onlyid: call.req._onlyid,
        _fromid: call.req._fromid
    })
}